

#import "FAMHomeFoodChangeView.h"
@interface FAMHomeFoodChangeView()
@property (weak, nonatomic) IBOutlet UIView *FAMNameBackView;
@property (weak, nonatomic) IBOutlet UIView *FAMGrossBackView;
@property (weak, nonatomic) IBOutlet UIButton *FAMDeleteButton;
@property (weak, nonatomic) IBOutlet UITextField *FAMFoodNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *FAMFoodWeightTextField;
@property (weak, nonatomic) IBOutlet UIImageView *FAMFoodImageView;


@end
@implementation FAMHomeFoodChangeView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"FAMHomeFoodChangeView" owner:self options:nil];
        self = array.firstObject;
        [self FAM_showUi];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
}
- (void)FAM_showUi{
    self.FAMNameBackView.layer.borderWidth = 1;
    self.FAMNameBackView.layer.borderColor = kCustom0xColor(0xBABABA, 1.0).CGColor;
    self.FAMGrossBackView.layer.borderWidth = 1;
    self.FAMGrossBackView.layer.borderColor = kCustom0xColor(0xBABABA, 1.0).CGColor;
    self.FAMDeleteButton.layer.borderWidth = 1;
    self.FAMDeleteButton.layer.borderColor = kCustom0xColor(0x191919, 1.0).CGColor;
}
- (IBAction)FAM_closeButtonClick:(UIButton *)sender {
    [self removeFromSuperview];
}
- (void)setFAMDate:(NSString *)FAMDate{
    _FAMDate = FAMDate;
}
- (void)setFAMFoodModel:(FAMFoodrecordModel *)FAMFoodModel{
    _FAMFoodModel = FAMFoodModel;
    self.FAMFoodNameTextField.text = FAMFoodModel.food_name;
    self.FAMFoodWeightTextField.text = FAMFoodModel.weight;
    [self.FAMFoodImageView sd_setImageWithURL:FAMFoodModel.image_url placeholderImage:[UIImage imageNamed:@"general_default_icon"]];
}
- (IBAction)FAM_deleteButtonClick:(UIButton *)sender {
    WeakSelf(self);
    [UIWindow.currentViewController showHudInKeyWindowWithHint:nil];
    [FAMHttpTool FAM_DeleteFoodmateFoodrecordWithId:_FAMFoodModel.ids success:^(FAMFoodmateUserdataModel * _Nonnull model, NSString * _Nonnull msg) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [UIWindow.currentViewController hideHudKeyWindow];
            [UIWindow.currentViewController performSelector:@selector(FAM_loadData:isFirst:) withObject:weakSelf.FAMDate withObject:@(YES)];
            [weakSelf removeFromSuperview];
        });
    } failure:^(NSString * _Nonnull errorInfo) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [UIWindow.currentViewController hideHudKeyWindow];
            [UIWindow.currentViewController showHint:errorInfo];
        });
    }];
}
- (IBAction)FAM_confirmButtonClick:(UIButton *)sender {
    if ([[NSString nullToString:self.FAMFoodNameTextField.text] isEqualToString:@""]) {
        [UIWindow.currentViewController showHint:@"Please enter the food name"];
        return;
    }
    if ([[NSString nullToString:self.FAMFoodWeightTextField.text] isEqualToString:@""]) {
        [UIWindow.currentViewController showHint:@"Please enter the total amount of food"];
        return;
    }
    WeakSelf(self);
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:_FAMFoodModel.ids forKey:@"id"];
    [dic setValue:self.FAMFoodNameTextField.text forKey:@"food_name"];
    [dic setValue:self.FAMFoodWeightTextField.text forKey:@"weight"];
    [UIWindow.currentViewController showHudInKeyWindowWithHint:nil];
    [FAMHttpTool FAM_PutFoodmateFoodrecordWithParameters:[NSMutableDictionary dictionaryWithDictionary:@{@"data":dic}] success:^(FAMFoodrecordModel * _Nonnull model, NSString * _Nonnull msg) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [UIWindow.currentViewController hideHudKeyWindow];
            [UIWindow.currentViewController performSelector:@selector(FAM_loadData:isFirst:) withObject:weakSelf.FAMDate withObject:@(YES)];
            [weakSelf removeFromSuperview];
        });
    } failure:^(NSString * _Nonnull errorInfo) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [UIWindow.currentViewController hideHudKeyWindow];
            [UIWindow.currentViewController showHint:errorInfo];
        });
    }];
}

@end
